<template>
  <div class="LoginNoPwd">
    <div
      class="content"
      v-loading="loading"
      element-loading-text="登录中..."
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(0, 0, 0, 0.8)"
    ></div>
  </div>
</template>

<script>
import axios from "axios";
import { getData, postData } from "@/request/http";
export default {
  name: "LoginNoPwd",
  data() {
    return {
      loading: true,
      token_type: "",
      access_token: "",
      username: "",
    };
  },
  components: {},
  computed: {},
  mounted() {
    this.token_type = this.$route.query.token_type;
    this.access_token = this.$route.query.access_token;
    this.username = this.$route.query.username;
    console.log(this.token_type);
    console.log(this.access_token);
    this.loginNoPwd();
  },
  methods: {
    //首字母转大写
    firstToUpper1(str) {
      if (str != null) {
        return str.replace(/\b(\w)(\w*)/g, function($0, $1, $2) {
          return $1.toUpperCase() + $2.toLowerCase();
        });
      }
    },
    //获取userinfo
    /* getUserInfo() {
      let Authorization =
        this.firstToUpper1(this.token_type) + " " + this.access_token;
      console.log(Authorization);
      axios
        .post(
          "http://10.238.252.81:8188/api/oauth2/use",
          {},
          {
            headers: {
              "Content-Type": "application/json;charset=utf8",
              Authorization: Authorization,
            },
          }
        )
        .then(function(res) {
          console.log(res);
          if (res.status_code == 1) {
            this.username = res.content.username;
            this.loading = false;
            this.loginNoPwd();
          } else {
            this.$router.go(0);
          }
        });
    }, */
    //无密登录
    loginNoPwd() {
      postData("/adminweb/sys/admin/login/sugon", {
        user_name: this.username,
        token_type: this.token_type,
        access_token: this.access_token,
      })
        .then((res) => {
          console.log(res);
          if (res.code == 0) {
            let pers = res.datas.permissions;
            this.$store.commit("login/setUser", res.datas.user_name);
            this.$store.commit("login/set_nick_name", res.datas.nick_name);
            this.$store.commit("login/set_mobile", res.datas.mobile);
            this.$store.commit("login/set_email", res.datas.email);
            this.$store.commit("login/set_permissions", JSON.stringify(pers));

            localStorage.setItem("username", res.datas.user_name);
            localStorage.setItem("token", res.datas.token);
            localStorage.setItem("nick_name", res.datas.nick_name);
            localStorage.setItem("mobile", res.datas.mobile);
            localStorage.setItem("email", res.datas.email);
            localStorage.setItem("token_expired", res.datas.token_expired);

            localStorage.setItem("permissions", JSON.stringify(pers));
            this.$router.push("/cameraPlay");
          } else {
            this.$message.error(res.code + "，" + res.msg);
          }
        })
        .catch((res) => {
          this.$message.error("error");
        });
    },
  },
};
</script>

<style scoped>
.LoginNoPwd {
  width: 100%;
  height: 100vh;
}
.content {
  width: 100%;
  height: 100%;
}
</style>
